<?php

if (!defined('BASEPATH'))
exit('No direct script access allowed');

class Listinggrid extends MX_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 * 	- or -
	 * 		http://example.com/index.php/welcome/index
	 * 	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	function Listinggrid() {
		parent::__construct();
		$this->load->model('lookupmodel');
		$this->load->model('helpermodel');
		//check is logged in
		$this->load->model('languagemodel');
		$this->load->model('projects/mdl_projectsmodel');
		$this->language = $this->languagemodel->is_language_using();
		$this->load->helper('MY_url_helper');
		$this->load->helper('text');
		
		//right side banner
		$this->load->model('banners/bannermodel');
	}

	public function index() {
		$this->lang->load('listing/grid', $this->language);
		$this->lang->load('listing/listing', $this->language);
		$this->lang->load('common/header', $this->language);

		//SEO
		$data['title'] = $this->lang->line('text_grid_title');
		$data['description'] = $this->lang->line('text_grid_description');
		$data['keywords'] = $this->lang->line('text_grid_keywords');

		$data['breadcrumbs'][] = array(
			'text'      => 'Home',
			'href'      => base_url()
		);

		$data['breadcrumbs'][] = array(
			'text'      => $this->lang->line('text_heading'),
			'href'      => base_url() . 'listing-grid.html?localorint=0&propertyfor=sale'
		);

		$data['module'] = "listinggrid";
		$data['view_file'] = "listinggrid";

		$this->load->model('listingmap/mdl_listingmapmodel');
		$options = array();
		
		$options['language'] = $this->config->item('config_language_id');
		$options['market'] = $this->config->item('market');

		//refer to Int listing searchtenure
		$localorInt = $this->input->get('localorint');
		$options['localorInt'] = $localorInt;
		$data['localorInt'] = $localorInt;
		//get Country ID based on Market ID
		if($this->input->get('country')) {
			$options['country'] = $this->input->get('country');
		} else {
			$options['country'] = $this->lookupmodel->getCountryId($this->config->item('market'));
		}
		 
		$cities = $this->input->get('cities');
		$valToArr = array();
		if($this->input->get('cities')) {
			if (!is_array($this->input->get('cities'))) {
				$valToArr = explode(',', $this->input->get('cities'));
			} else {
				$valToArr = $this->input->get('cities');
			}
		}
		$data['cities'] = $valToArr;

		if ($this->helpermodel->check_is_array($cities)) {
			$options['cities'] = implode(',', $this->input->get('cities'));
		} else {
			if($cities != "") {
				$options['cities'] = $this->input->get('cities');
			} else {
				$options['cities'] = NULL;
			}
		}

		$locations = $this->input->get('locations');
		$data['locations'] = $locations;

		if ($this->helpermodel->check_is_array($locations)) {
			$options['locations'] = implode(',', $this->input->get('locations'));
		} else {
			$options['locations'] = NULL;
		}

		$subarea = $this->input->get('chkSubArea');

		if ($this->helpermodel->check_is_array($subarea)) {
			$options['subarea'] = implode(',', $this->input->get('chkSubArea'));
		} else {
			$options['subarea'] = NULL;
		}

		$options['list_type'] = $this->input->get('list_type');
		$data['list_type'] = $options['list_type'];
		if($options['list_type'] != "") {
			$options['list_type'] = $this->input->get('list_type');
		} else {
			$options['list_type'] = NULL;
		}
		
		if($options['list_type'] == "land") {
			//SEO
			$data['title'] = $this->lang->line('text_grid_land_title');
			$data['description'] = $this->lang->line('text_grid_description');
			$data['keywords'] = $this->lang->line('text_grid_keywords');
		}

		$options['propertyfor'] = $this->input->get('propertyfor');
		$data['propertyfor'] = $options['propertyfor'];
		if($options['propertyfor'] != "") {
			$options['propertyfor'] = $this->input->get('propertyfor');
		} else {
			$options['propertyfor'] = NULL;
		}

		$options['propertytypeclss'] = $this->input->get('ddlPropertyClss');
		$data['propertytypeclss'] = $options['propertytypeclss'];
		if($options['propertytypeclss'] != "") {
			$options['propertytypeclss'] = $this->input->get('ddlPropertyClss');
		} else {
			$options['propertytypeclss'] = NULL;
		}

		$options['propertytype'] = $this->input->get('ddlPropertyType');
		$data['propertytype'] = $options['propertytype'];
		if($options['propertytype'] != "") {
			$options['propertytype'] = $this->input->get('ddlPropertyType');
		} else {
			$options['propertytype'] = NULL;
		}

		$options['price_min'] = $this->input->get('price_min');
		if($options['price_min'] != "" && $options['price_min'] != "Min") {
			$options['price_min'] = $this->input->get('price_min');
			$data['price_min'] = $options['price_min'];
		} else {
			$options['price_min'] = NULL;
			$data['price_min'] = false;
		}

		$options['price_max'] = $this->input->get('price_max');		
		if($options['price_max'] != "" && $options['price_max'] != "Max") {
			$options['price_max'] = $this->input->get('price_max');
			$data['price_max'] = $options['price_max'];
		} else {
			$options['price_max'] = NULL;
			$data['price_max'] = false;
		}

		$options['size_min'] = $this->input->get('size_min');		
		if($options['size_min'] != "" && $options['size_min'] != "Min") {
			$options['size_min'] = $this->input->get('size_min');
			$data['size_min'] = $options['size_min'];
		} else {
			$options['size_min'] = NULL;
			$data['size_min'] = false;
		}

		$options['size_max'] = $this->input->get('size_max');
		if($options['size_max'] != "" && $options['size_max'] != "Max") {
			$options['size_max'] = $this->input->get('size_max');
			$data['size_max'] = $options['size_max'];
		} else {
			$options['size_max'] = NULL;
			$data['size_max'] = false;
		}

		$options['bedrooms'] = $this->input->get('bedrooms');
		$data['bedrooms'] = $options['bedrooms'];
		if($options['bedrooms'] != "") {
			$options['bedrooms'] = $this->input->get('bedrooms');
		} else {
			$options['bedrooms'] = NULL;
		}
		
		//min bed
		$options['bedroom_min'] = $this->input->get('bedroom_min');
		if($options['bedroom_min'] != "" && $options['bedroom_min'] != "Min") {
			$options['bedroom_min'] = $this->input->get('bedroom_min');
			$data['bedroom_min'] = $options['bedroom_min'];
		} else {
			$options['bedroom_min'] = NULL;
			$data['bedroom_min'] = false;
		}
		
		//max bed
		$options['bedroom_max'] = $this->input->get('bedroom_max');
		if($options['bedroom_max'] != "" && $options['bedroom_max'] != "Max") {
			$options['bedroom_max'] = $this->input->get('bedroom_max');
			$data['bedroom_max'] = $options['bedroom_max'];
		} else {
			$options['bedroom_max'] = NULL;
			$data['bedroom_max'] = false;
		}

		$options['bathrooms'] = $this->input->get('bathrooms');
		$data['bathrooms'] = $options['bathrooms'];
		if($options['bathrooms'] != "") {
			$options['bathrooms'] = $this->input->get('bathrooms');
		} else {
			$options['bathrooms'] = NULL;
		}
		
		//min bath
		$options['bathroom_min'] = $this->input->get('bathroom_min');
		if($options['bathroom_min'] != "" && $options['bathroom_min'] != "Min") {
			$options['bathroom_min'] = $this->input->get('bathroom_min');
			$data['bathroom_min'] = $options['bathroom_min'];
		} else {
			$options['bathroom_min'] = NULL;
			$data['bathroom_min'] = false;
		}
		
		//max bath
		$options['bathroom_max'] = $this->input->get('bathroom_max');
		if($options['bathroom_max'] != "" && $options['bathroom_max'] != "Max") {
			$options['bathroom_max'] = $this->input->get('bathroom_max');
			$data['bathroom_max'] = $options['bathroom_max'];
		} else {
			$options['bathroom_max'] = NULL;
			$data['bathroom_max'] = false;
		}
		
		$options['keyword'] = $this->input->get('keyword');
		if($options['keyword'] != "" && $options['keyword'] != "Enter keyword of Properties") {
			$options['keyword'] = $this->input->get('keyword');
			$data['keyword'] = $options['keyword'];
		} else {
			$options['keyword'] = NULL;
			$data['keyword'] = false;
		}

		$options['listing_id'] = NULL;
		//Features
		$features = $this->input->get('feature_category');

		if ($this->helpermodel->check_is_array($features)) {
			$options['features'] = implode(',', $features);
		} else {
			$options['features'] = NULL;
		}

		//Facilities
		$facilities = $this->input->get('facilities_category');

		if ($this->helpermodel->check_is_array($facilities)) {
			$options['facilities'] = implode(',', $facilities);
		} else {
			$options['facilities'] = NULL;
		}
		
		//tenure
		$options['tenu'] = $this->input->get('tenure');
		$data['tenu'] = $options['tenu'];
		if($options['tenu'] != "") {
			$options['tenu'] = $this->input->get('tenure');
		} else {
			$options['tenu'] = NULL;
		}

		if ($this->input->get('sort')) {
			$sort = $this->input->get('sort');
			$data['sort'] = $sort;
			if ($sort == 'date-listed') {
				$options['orderby'] = "ListedOnUtc DESC";
			}

			if ($sort == 'date-listed-old-to-new') {
				$options['orderby'] = "ListedOnUtc ASC";
			}

			if ($sort == 'price-low-to-high') {
				$options['orderby'] = "Price_Min ASC";
			}

			if ($sort == 'price-high-to-low') {
				$options['orderby'] = "Price_Min DESC";
			}

			if ($sort == 'size-low-to-high') {
				$options['orderby'] = "Size_Min ASC";
			}

			if ($sort == 'size-high-to-low') {
				$options['orderby'] = "Size_Min DESC";
			}
		} else {
			$options['orderby'] = 'ListedOnUtc DESC';
			$data['sort'] = 'date-listed';
		}

		if ($this->input->get('page')) {
			$options['page'] = $this->input->get('page');
		}
		else{
			$options['page'] = 1;
		}

		if($this->input->get('limit')) {
			$limit = $this->input->get('limit');
		} else {
			$limit = PER_PAGE;
		}

		$data['limit'] = $limit;

		$options['offset'] = ($options['page'] - 1) * $limit;
		$options['limit'] = $limit + $options['offset'];

		//$results = $this->mdl_listingmapmodel->getListingWithWhere($options);
		$results = $this->mdl_listingmapmodel->getProjectListing($options);

		$data['listings'] = $results;

		// paging
		if (isset($results[0])) {
			$data['total_count'] = $results[0]['total'];
		} else {
			$data['total_count'] = 0;
		}

		//$url = full_url();
		$url = '';

		$url .= '&localorint=' . $this->input->get('localorint');

		if($this->input->get('propertyfor')) {
			$url .= '&propertyfor=' . $this->input->get('propertyfor');
		}

		if($this->input->get('list_type')) {
			$url .= '&list_type=' . $this->input->get('list_type');
		}

		if($this->input->get('ddlPropertyType')) {
			$url .= '&ddlPropertyType=' . $this->input->get('ddlPropertyType');
		}

		$cityStr = "";
		if($this->input->get('cities')) {
			 
		}

		if($this->input->get('cities')) {
			$cityArr = array();
			if(!is_array($this->input->get('cities'))) {
				$cityArr = explode(',', $this->input->get('cities'));
			} else {
				$cityArr = $this->input->get('cities');
			}

			foreach ($cityArr as $key => $value) {
				$cityStr .= '&cities[]=' . $value;
			}
			//$cityStr = substr($cityStr, 0, -1);
			$url .= $cityStr;
		}

		$locationStr = "";
		if($this->input->get('locations')) {
			foreach ($this->input->get('locations') as $key => $value) {
				$locationStr .= '&locations[]=' . $value;
			}
			//$locationStr = substr($locationStr, 0, -1);
			$url .= $locationStr;
		}

		$subareaStr = "";
		if($this->input->get('chkSubArea')) {
			foreach ($this->input->get('chkSubArea') as $key => $value) {
				$subareaStr .= '&chkSubArea[]=' . $value;
			}
			//$subareaStr = substr($subareaStr, 0, -1);
			$url .= $subareaStr;
		}

		if($this->input->get('price_min')) {
			$url .= '&price_min=' . $this->input->get('price_min');
		}

		if($this->input->get('price_max')) {
			$url .= '&price_max=' . $this->input->get('price_max');
		}

		if($this->input->get('size_min')) {
			$url .= '&size_min=' . $this->input->get('size_min');
		}

		if($this->input->get('size_max')) {
			$url .= '&size_max=' . $this->input->get('size_max');
		}

		if($this->input->get('bedrooms')) {
			$url .= '&bedrooms=' . $this->input->get('bedrooms');
		}

		if($this->input->get('bathrooms')) {
			$url .= '&bathrooms=' . $this->input->get('bathrooms');
		}

		if($this->input->get('keyword')) {
			$url .= '&keyword=' . urlencode(html_entity_decode($this->input->get('keyword'), ENT_QUOTES, 'UTF-8'));;
		}

		if ($this->input->get('sort')) {
			$url .= '&sort=' . $this->input->get('sort');
		}

		if ($this->input->get('page')) {
			$url .= '&page=' . $this->input->get('page');
		}

		if($this->input->get('limit')) {
			$url .= '&limit=' . $this->input->get('limit');
		}

		//Scope For Sorting
		$url = '';
		$url .= '&localorint=' . $this->input->get('localorint');

		if($this->input->get('propertyfor')) {
			$url .= '&propertyfor=' . $this->input->get('propertyfor');
		}

		if($this->input->get('list_type')) {
			$url .= '&list_type=' . $this->input->get('list_type');
		}

		if($this->input->get('ddlPropertyType')) {
			$url .= '&ddlPropertyType=' . $this->input->get('ddlPropertyType');
		}

		$cityStr = "";
		if($this->input->get('cities')) {
			$cityArr = array();
			if(!is_array($this->input->get('cities'))) {
				$cityArr = explode(',', $this->input->get('cities'));
			} else {
				$cityArr = $this->input->get('cities');
			}

			foreach ($cityArr as $key => $value) {
				$cityStr .= '&cities[]=' . $value;
			}
			//$cityStr = substr($cityStr, 0, -1);
			$url .= $cityStr;
		}

		$locationStr = "";
		if($this->input->get('locations')) {
			foreach ($this->input->get('locations') as $key => $value) {
				$locationStr .= '&locations[]=' . $value;
			}
			//$locationStr = substr($locationStr, 0, -1);
			$url .= $locationStr;
		}

		$subareaStr = "";
		if($this->input->get('chkSubArea')) {
			foreach ($this->input->get('chkSubArea') as $key => $value) {
				$subareaStr .= '&chkSubArea[]=' . $value;
			}
			//$subareaStr = substr($subareaStr, 0, -1);
			$url .= $subareaStr;
		}

		if($this->input->get('price_min')) {
			$url .= '&price_min=' . $this->input->get('price_min');
		}

		if($this->input->get('price_max')) {
			$url .= '&price_max=' . $this->input->get('price_max');
		}

		if($this->input->get('size_min')) {
			$url .= '&size_min=' . $this->input->get('size_min');
		}

		if($this->input->get('size_max')) {
			$url .= '&size_max=' . $this->input->get('size_max');
		}

		if($this->input->get('bedrooms')) {
			$url .= '&bedrooms=' . $this->input->get('bedrooms');
		}

		if($this->input->get('bathrooms')) {
			$url .= '&bathrooms=' . $this->input->get('bathrooms');
		}

		if($this->input->get('keyword')) {
			$url .= '&keyword=' . urlencode(html_entity_decode($this->input->get('keyword'), ENT_QUOTES, 'UTF-8'));;
		}

		if($this->input->get('limit')) {
			$url .= '&limit=' . $this->input->get('limit');
		}

		$data['listing_rows'] = base_url() . 'listing-rows.html?'. $url;
		$data['listing_grid'] = base_url() . 'listing-grid.html?'. $url;

		$data['sorts'] = array();
		$data['sorts'][] = array(
		'text'  => $this->lang->line('text_default'),
		'value' => 'date-listed',
		'href'  => base_url() . 'listing-grid.html?sort=date-listed' . $url
		);
			
		$data['sorts'][] = array(
		'text'  => $this->lang->line('text_date_old_to_new'),
		'value' => 'date-listed-old-to-new',
		'href'  => base_url() . 'listing-grid.html?sort=date-listed-old-to-new' . $url
		);

		$data['sorts'][] = array(
		'text'  => $this->lang->line('text_price_low_to_high'),
		'value' => 'price-low-to-high',
		'href'  => base_url() . 'listing-grid.html?sort=price-low-to-high' . $url
		);

		$data['sorts'][] = array(
		'text'  => $this->lang->line('text_price_high_to_low'),
		'value' => 'price-high-to-low',
		'href'  => base_url() . 'listing-grid.html?sort=price-high-to-low' . $url
		);

		$data['sorts'][] = array(
		'text'  => $this->lang->line('text_size_low_to_high'),
		'value' => 'size-low-to-high',
		'href'  => base_url() . 'listing-grid.html?sort=size-low-to-high' . $url
		);

		$data['sorts'][] = array(
		'text'  => $this->lang->line('text_size_high_to_low'),
		'value' => 'size-high-to-low',
		'href'  => base_url() . 'listing-grid.html?sort=size-high-to-low' . $url
		);

		//Scope for Limiting
		$url = '';
		$url .= '&localorint=' . $this->input->get('localorint');

		if($this->input->get('propertyfor')) {
			$url .= '&propertyfor=' . $this->input->get('propertyfor');
		}

		if($this->input->get('list_type')) {
			$url .= '&list_type=' . $this->input->get('list_type');
		}

		if($this->input->get('ddlPropertyType')) {
			$url .= '&ddlPropertyType=' . $this->input->get('ddlPropertyType');
		}

		$cityStr = "";
		if($this->input->get('cities')) {
			$cityArr = array();
			if(!is_array($this->input->get('cities'))) {
				$cityArr = explode(',', $this->input->get('cities'));
			} else {
				$cityArr = $this->input->get('cities');
			}

			foreach ($cityArr as $key => $value) {
				$cityStr .= '&cities[]=' . $value;
			}
			//$cityStr = substr($cityStr, 0, -1);
			$url .= $cityStr;
		}

		$locationStr = "";
		if($this->input->get('locations')) {
			foreach ($this->input->get('locations') as $key => $value) {
				$locationStr .= '&locations[]=' . $value;
			}
			//$locationStr = substr($locationStr, 0, -1);
			$url .= $locationStr;
		}

		$subareaStr = "";
		if($this->input->get('chkSubArea')) {
			foreach ($this->input->get('chkSubArea') as $key => $value) {
				$subareaStr .= '&chkSubArea[]=' . $value;
			}
			//$subareaStr = substr($subareaStr, 0, -1);
			$url .= $subareaStr;
		}

		if($this->input->get('price_min')) {
			$url .= '&price_min=' . $this->input->get('price_min');
		}

		if($this->input->get('price_max')) {
			$url .= '&price_max=' . $this->input->get('price_max');
		}

		if($this->input->get('size_min')) {
			$url .= '&size_min=' . $this->input->get('size_min');
		}

		if($this->input->get('size_max')) {
			$url .= '&size_max=' . $this->input->get('size_max');
		}

		if($this->input->get('bedrooms')) {
			$url .= '&bedrooms=' . $this->input->get('bedrooms');
		}

		if($this->input->get('bathrooms')) {
			$url .= '&bathrooms=' . $this->input->get('bathrooms');
		}

		if($this->input->get('keyword')) {
			$url .= '&keyword=' . urlencode(html_entity_decode($this->input->get('keyword'), ENT_QUOTES, 'UTF-8'));;
		}

		if ($this->input->get('sort')) {
			$url .= '&sort=' . $this->input->get('sort');
		}

		if ($this->input->get('page')) {
			$url .= '&page=' . $this->input->get('page');
		}

		$data['listing_rows'] = base_url() . 'listing-rows.html?'. $url;
		$data['listing_grid'] = base_url() . 'listing-grid.html?'. $url;

		$data['limits'] = array();

		$limits = array_unique(array(PER_PAGE, 20, 30, 40));

		sort($limits);

		foreach($limits as $limits){
			$data['limits'][] = array(
			'text'  => $limits,
			'value' => $limits,
			'href'  => base_url() . 'listing-grid.html?' . $url . '&limit=' . $limits
			);
		}

		//Scope for Paging
		$url = '';
		$url .= '&localorint=' . $this->input->get('localorint');

		//echo $url; die();

		if($this->input->get('propertyfor')) {
			$url .= '&propertyfor=' . $this->input->get('propertyfor');
		}

		if($this->input->get('list_type')) {
			$url .= '&list_type=' . $this->input->get('list_type');
		}

		if($this->input->get('ddlPropertyType')) {
			$url .= '&ddlPropertyType=' . $this->input->get('ddlPropertyType');
		}

		$cityStr = "";
		if($this->input->get('cities')) {
			$cityArr = array();
			if(!is_array($this->input->get('cities'))) {
				$cityArr = explode(',', $this->input->get('cities'));
			} else {
				$cityArr = $this->input->get('cities');
			}

			foreach ($cityArr as $key => $value) {
				$cityStr .= '&cities[]=' . $value;
			}
			//$cityStr = substr($cityStr, 0, -1);
			$url .= $cityStr;
		}

		$locationStr = "";
		if($this->input->get('locations')) {
			foreach ($this->input->get('locations') as $key => $value) {
				$locationStr .= '&locations[]=' . $value;
			}
			//$locationStr = substr($locationStr, 0, -1);
			$url .= $locationStr;
		}

		$subareaStr = "";
		if($this->input->get('chkSubArea')) {
			foreach ($this->input->get('chkSubArea') as $key => $value) {
				$subareaStr .= '&chkSubArea[]=' . $value;
			}
			//$subareaStr = substr($subareaStr, 0, -1);
			$url .= $subareaStr;
		}

		if($this->input->get('price_min')) {
			$url .= '&price_min=' . $this->input->get('price_min');
		}

		if($this->input->get('price_max')) {
			$url .= '&price_max=' . $this->input->get('price_max');
		}

		if($this->input->get('size_min')) {
			$url .= '&size_min=' . $this->input->get('size_min');
		}

		if($this->input->get('size_max')) {
			$url .= '&size_max=' . $this->input->get('size_max');
		}

		if($this->input->get('bedrooms')) {
			$url .= '&bedrooms=' . $this->input->get('bedrooms');
		}

		if($this->input->get('bathrooms')) {
			$url .= '&bathrooms=' . $this->input->get('bathrooms');
		}

		if($this->input->get('keyword')) {
			$url .= '&keyword=' . urlencode(html_entity_decode($this->input->get('keyword'), ENT_QUOTES, 'UTF-8'));;
		}

		if ($this->input->get('sort')) {
			$url .= '&sort=' . $this->input->get('sort');
		}

		if ($this->input->get('limit')) {
			$url .= '&limit=' . $this->input->get('limit');
		}

		$data['listing_rows'] = base_url() . 'listing-rows.html?'. $url;
		$data['listing_grid'] = base_url() . 'listing-grid.html?'. $url;


		$this->load->library('pagination');
		$config['base_url'] = base_url() . "listing-grid.html?" . $url;
		$config['enable_query_strings'] = TRUE;
		$config['page_query_string'] = TRUE;
		$config['use_page_numbers'] = TRUE;
		$config['query_string_segment'] = 'page';
		$config['per_page'] = $limit;
		$config['total_rows'] = $data['total_count'];


		$this->pagination->initialize($config);
		$data['paginator'] = $this->pagination->create_links();
		// end paging
		
		//get banner for right side
		$data['banner'] = $this->bannermodel->getBanners('Result', $this->config->item('market'), $this->config->item('config_language_id'));
		
		echo Modules::run('template/default_template', $data);
	}

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */